package com.cz.nongyetongv5.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.bean.ChanPinJianCeRecord;
import com.cz.nongyetongv5.bean.DuChaXunChaRecord;
import com.cz.nongyetongv5.bean.JianChaDanWei;
import com.cz.nongyetongv5.bean.JianChaJieGuo;
import com.cz.nongyetongv5.bean.JianChaRenYuan;
import com.cz.nongyetongv5.bean.JianDuJianChaRecord;
import com.cz.nongyetongv5.bean.Photo;
import com.cz.nongyetongv5.bean.YangPin;
import com.cz.nongyetongv5.custom.TextProgressBar;
import com.cz.nongyetongv5.custom.wheelview.Utils;
import com.cz.nongyetongv5.task.UploadDaiShangChuanTask;
import com.cz.nongyetongv5.utils.CharsetUtil;
import com.cz.nongyetongv5.utils.SerliazableUtil;
import com.iflytek.cloud.thirdparty.V;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class JiandujianchaListAdapter extends BaseAdapter {

    // 上下文对象
    private Context context = null;

    /*
     * 所有的数据
     */
    private List<JianDuJianChaRecord> jianDuJianChaRecords = null;

    /**
     * 单机事件监听对象
     */
    private onClickItemListener onClickItemListener;

    public JiandujianchaListAdapter(Context context, List<JianDuJianChaRecord> datas) {
        this.context = context;
        this.jianDuJianChaRecords = datas;
    }


    public void setOnClickItemListener(JiandujianchaListAdapter.onClickItemListener onClickItemListener) {
        this.onClickItemListener = onClickItemListener;
    }

    public interface onClickItemListener {
        void onClickItemListener(int position, View v);
    }

    @Override
    public int getCount() {
        return jianDuJianChaRecords == null ? 0 : jianDuJianChaRecords.size();
    }

    @Override
    public Object getItem(int arg0) {
        return jianDuJianChaRecords.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    public void setDatas(List<JianDuJianChaRecord> datas) {
        this.jianDuJianChaRecords = datas;
        this.notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            final LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.daishangchuan_item_layout, null);
            holder = new ViewHolder();
            holder.ivDaiShangChuanImage = convertView.findViewById(R.id.ivDaiShangChuanImage);
            holder.tvDaiShangChuanRiQi = convertView.findViewById(R.id.tvDaiShangChuanRiQi);
            holder.tvDaiShangChuanDanWei = convertView.findViewById(R.id.tvDaiShangChuanDanWei);
            holder.tvDaiShangChuanJieGuo = convertView.findViewById(R.id.tvDaiShangChuanJieGuo);
            holder.btnDaiShangChuanDelete = convertView.findViewById(R.id.btnDaiShangChuanDelete);
            holder.btnDaiShangChuanEdit = convertView.findViewById(R.id.btnDaiShangChuanEdit);
            holder.btnDaiShangChuanUpload = convertView.findViewById(R.id.btnDaiShangChuanUpload);
            holder.llJianChaDanWeiLayout = convertView.findViewById(R.id.llJianChaDanWeiLayout);
            holder.tvJianChaRenYuan = convertView.findViewById(R.id.tvJianChaRenYuan);
            holder.tvTitle1 = convertView.findViewById(R.id.title1);
            holder.tvTitle2 = convertView.findViewById(R.id.title2);
            holder.llMenuLayout = convertView.findViewById(R.id.llMenuLayout);
            holder.llProgressLayout = convertView.findViewById(R.id.llProgressLayout);
            holder.btnBackUploadMenu = convertView.findViewById(R.id.btnBackUploadMenu);
            holder.tpUploadProgress = convertView.findViewById(R.id.tpUploadProgress);
            holder.root = convertView.findViewById(R.id.root);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        // 获得监督检查的记录
        JianDuJianChaRecord jianDuJianChaRecord = jianDuJianChaRecords.get(position);
        if (jianDuJianChaRecord.isUpload()) {
            if (jianDuJianChaRecords.get(position).isBack()) {
                holder.llMenuLayout.setVisibility(View.VISIBLE);
                holder.llProgressLayout.setVisibility(View.GONE);
                holder.btnBackUploadMenu.setVisibility(View.VISIBLE);
            } else {
                holder.llMenuLayout.setVisibility(View.GONE);
                // 设置进度条为显示
                holder.llProgressLayout.setVisibility(View.VISIBLE);
                // 设置进度条为不显示
                holder.btnBackUploadMenu.setVisibility(View.GONE);
                //                holder.tpUploadProgress.setProgress(0);
                //                holder.tpUploadProgress.setText("0%");
            }
            holder.tpUploadProgress.setText(jianDuJianChaRecords.get(position).getProgress() + "%");
            // 默认为百分之0
            //            jianDuJianChaRecords.get(position).setProgress(100);
            if (jianDuJianChaRecords.get(position).getProgress() >= 100) {
                holder.btnBackUploadMenu.setVisibility(View.VISIBLE);
                holder.tpUploadProgress.setText("上传成功");
            }else {
                if (jianDuJianChaRecords.get(position).getMsg() != null && !jianDuJianChaRecords.get(position).getMsg().isEmpty()) {
                    holder.tpUploadProgress.setText(jianDuJianChaRecords.get(position).getMsg());
                }
            }
            holder.btnBackUploadMenu.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onClickItemListener != null) {
                        onClickItemListener.onClickItemListener(position, v);
                    }
                }
            });
        } else {
            holder.llMenuLayout.setVisibility(View.VISIBLE);
            holder.llProgressLayout.setVisibility(View.GONE);
            holder.btnBackUploadMenu.setVisibility(View.VISIBLE);

        }

        // 显示照片
        if (jianDuJianChaRecord.getUploadPhoto().size() > 0) {

            Photo firstPhoto = jianDuJianChaRecord.getUploadPhoto().get(0);

            // 如果是有小图的
            if (firstPhoto.getSmallPhotoPath() != null
                    && (firstPhoto.getSmallPhotoPath() instanceof String)) {

                //                    ImageView ivDaiShangChuanImage = (ImageView) daiShangChuanLinearLayout
                //                            .findViewById(R.id.ivDaiShangChuanImage);

                // 进行图片的显示
                BitmapFactory.Options opts = new BitmapFactory.Options();
                opts.inSampleSize = 2;

                Bitmap image = BitmapFactory.decodeFile((String) firstPhoto
                        .getSmallPhotoPath());
                holder.ivDaiShangChuanImage.setImageBitmap(image);
                holder.ivDaiShangChuanImage.setScaleType(ScaleType.CENTER_CROP);
            }

        } else {
            // 如果没有则显示签名
            //                ImageView ivDaiShangChuanImage = (ImageView) daiShangChuanLinearLayout
            //                        .findViewById(R.id.ivDaiShangChuanImage);

            BitmapFactory.Options opts = new BitmapFactory.Options();

            opts.inSampleSize = 2;

            Bitmap image = BitmapFactory.decodeFile(
                    jianDuJianChaRecord.getQianMingPath(), opts);

            holder.ivDaiShangChuanImage.setImageBitmap(image);

            holder.ivDaiShangChuanImage.setScaleType(ScaleType.CENTER_CROP);
        }

        try {

            // 时间的设置
            Date time = new SimpleDateFormat("yyyy-MM-dd")
                    .parse(jianDuJianChaRecord.getTime());

            //                TextView tvDaiShangChuanRiQi = (TextView) daiShangChuanLinearLayout
            //                        .findViewById(R.id.tvDaiShangChuanRiQi);

            String timeStr = new SimpleDateFormat("yyyy.MM.dd")
                    .format(time);

            holder.tvDaiShangChuanRiQi.setText(timeStr);

        } catch (Exception e) {
        }

        // 设置被检单位
        //            TextView tvDaiShangChuanDanWei = (TextView) daiShangChuanLinearLayout
        //                    .findViewById(R.id.tvDaiShangChuanDanWei);

        holder.tvDaiShangChuanDanWei.setText(jianDuJianChaRecord
                .getBeiJianDanWei() == null ? "" : jianDuJianChaRecord
                .getBeiJianDanWei().getName());

        // 合格数量
        int heGeCount = 0;

        // 不合格数量
        int buHeGeCount = 0;

        // 基本合格数量
        int jiBenHeGeCount = 0;

        // 统计基本合格和不合格的数量
        for (JianChaJieGuo jianChaJieGuo : jianDuJianChaRecord
                .getJianChaJieGuos()) {

            if (jianChaJieGuo != null && jianChaJieGuo.getId() != null) {

                if (buHeGeCount > 0) {

                    break;
                }

                if (jianChaJieGuo.getId() == 1) {

                    heGeCount++;
                }

                if (jianChaJieGuo.getId() == 2) {

                    jiBenHeGeCount++;
                }

                if (jianChaJieGuo.getId() == 3) {

                    buHeGeCount++;
                }

            }

        }

        // 继续统计,包括日常监管
        if (jianDuJianChaRecord.getRiChangJianGuan() != null) {

            if (jianDuJianChaRecord.getRiChangJianGuan().getId() == 1) {

                heGeCount++;

            }

            if (jianDuJianChaRecord.getRiChangJianGuan().getId() == 2) {

                jiBenHeGeCount++;
            }

            if (jianDuJianChaRecord.getRiChangJianGuan().getId() == 3) {

                buHeGeCount++;
            }

        }

        String result = "";

        int color;

        if (buHeGeCount > 0) {

            result = "不合格";
            color = Color.parseColor("#ffff0000");
        } else if (jiBenHeGeCount > 0) {
            result = "基本合格";
            color = Color.parseColor("#F4C82D");
        } else {
            result = "合格";
            color = Color.parseColor("#32A35E");
        }

        // 进行检查结果的显示
        //            TextView tvDaiShangChuanJieGuo = (TextView) daiShangChuanLinearLayout
        //                    .findViewById(R.id.tvDaiShangChuanJieGuo);

        holder.tvDaiShangChuanJieGuo.setText(result);
        holder.tvDaiShangChuanJieGuo.setTextColor(color);
        holder.btnDaiShangChuanDelete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickItemListener != null) {
                    onClickItemListener.onClickItemListener(position, v);
                }
            }
        });

        //            Button btnDaiShangChuanEdit = (Button) daiShangChuanLinearLayout
        //                    .findViewById(R.id.btnDaiShangChuanEdit);
        //        holder.btnDaiShangChuanEdit.setTag(position);
        //        holder.btnDaiShangChuanEdit.setOnClickListener(onClickListener);
        holder.btnDaiShangChuanEdit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickItemListener != null) {
                    onClickItemListener.onClickItemListener(position, v);
                }
            }
        });

        //            Button btnDaiShangChuanUpload = (Button) daiShangChuanLinearLayout
        //                    .findViewById(R.id.btnDaiShangChuanUpload);
        //        holder.btnDaiShangChuanUpload.setTag(position);
        //        holder.btnDaiShangChuanUpload.setOnClickListener(onClickListener);
        holder.btnDaiShangChuanUpload.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickItemListener != null) {
                    onClickItemListener.onClickItemListener(position, v);
                }
            }
        });


        // 遍历添加检查单位
        //            ViewGroup llJianChaDanWeiLayout = (ViewGroup) daiShangChuanLinearLayout
        //                    .findViewById(R.id.llJianChaDanWeiLayout);

        holder.llJianChaDanWeiLayout.removeAllViews();

        for (int i = 0; i < jianDuJianChaRecord.getJianChaDanWeis().size(); i++) {

            JianChaDanWei jianChaDanWei = jianDuJianChaRecord
                    .getJianChaDanWeis().get(i);

            TextView tvJianChaDanWei = new TextView(context);
            tvJianChaDanWei.setLayoutParams(new LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            tvJianChaDanWei.setTextColor(Color.parseColor("#36495C"));
            tvJianChaDanWei.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);

            int padding = Utils.dip2px(context, 10);

            tvJianChaDanWei.setPadding(padding, padding, padding, padding);

            tvJianChaDanWei.setFocusable(false);
            tvJianChaDanWei.setFocusableInTouchMode(false);
            tvJianChaDanWei.setText(jianChaDanWei.getName());

            holder.llJianChaDanWeiLayout.addView(tvJianChaDanWei);

            // 不是最后一个元素才绘制分割线
            if (i < jianDuJianChaRecord.getJianChaDanWeis().size() - 1) {

                View sperator = new View(context);
                sperator.setLayoutParams(new LayoutParams(
                        LayoutParams.MATCH_PARENT, 1));
                sperator.setBackgroundColor(Color.parseColor("#C4C2C0"));
                sperator.setClickable(false);
                holder.llJianChaDanWeiLayout.addView(sperator);

            }

        }

        // 遍历添加检查单位
        //            TextView tvJianChaRenYuan = (TextView) daiShangChuanLinearLayout
        //                    .findViewById(R.id.tvJianChaRenYuan);

        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < jianDuJianChaRecord.getJianChaReYuans().size(); i++) {

            JianChaRenYuan jianChaRenYuan = jianDuJianChaRecord
                    .getJianChaReYuans().get(i);

            sb.append(jianChaRenYuan.getName());

            if (i < jianDuJianChaRecord.getJianChaReYuans().size() - 1) {

                sb.append("、\t");
            }
        }

        holder.tvJianChaRenYuan.setText(sb.toString());


        return convertView;
    }

    /**
     * 删除
     *
     * @param position
     */
    public void remove(int position) {
        this.jianDuJianChaRecords.remove(position);
        this.notifyDataSetChanged();
    }

    public void remove(Object record) {
        remove(jianDuJianChaRecords.indexOf(record));
    }

    static class ViewHolder {
        ImageView ivDaiShangChuanImage;//图片
        TextView tvDaiShangChuanRiQi;//时间
        TextView tvDaiShangChuanDanWei;//被检单位
        TextView tvDaiShangChuanJieGuo;//检查结果
        Button btnDaiShangChuanDelete;//删除
        Button btnDaiShangChuanEdit;//编辑
        Button btnDaiShangChuanUpload;//上传
        ViewGroup llJianChaDanWeiLayout;//检查单位
        TextView tvJianChaRenYuan;//检查人员
        TextView tvTitle1;
        TextView tvTitle2;
        ViewGroup llMenuLayout;
        ViewGroup llProgressLayout;
        Button btnBackUploadMenu;
        TextProgressBar tpUploadProgress;
        FrameLayout root;
    }
}
